package com.learning.spring.cloud.alibaba.nacos.registraor.detail.web.controller;

import com.learning.spring.cloud.alibaba.nacos.registraor.detail.web.service.OrderFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * ClassName: UserController
 * Description:
 * Date: 2021/7/25 23:03 【需求编号】
 *
 * @author Sam Sho
 * @version V1.0.0
 */
@Slf4j
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    OrderFeignService orderFeignService;

    /**
     * 使用 Feign 访问
     * http://localhost:8001/user/query/1
     *
     * @param id
     * @return
     */
    @GetMapping("/query/{id}")
    public List<String> queryOrderByUserId(@PathVariable("id") Long id) {
        return orderFeignService.queryOrderByUserId(id);
    }


    @Resource
    RestTemplate restTemplate;

    /**
     * 使用 Ribbon 访问
     * http://localhost:8001/user/ribbon/query/1
     *
     * @param id
     * @return
     */
    @GetMapping("/ribbon/query/{id}")
    public List<String> selectByPrimaryKey(@PathVariable("id") Long id) {
        // 访问订单服务
        return this.restTemplate.getForObject("http://02-ms-nacos-order/order/query/user/" + id, List.class);
    }
}
